//
//  KATShape.m
//  KATFramework
//
//  Created by Kantice on 16/6/21.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATShape.h"
#import "KATMath.h"

NSString * const kShapeKeyType=@"shape_key_type";
NSString * const kShapeKeyMode=@"shape_key_mode";
NSString * const kShapeKeyModeGroup=@"shape_key_mode_group";
NSString * const kShapeKeyStrokeStart=@"shape_key_stroke_start";
NSString * const kShapeKeyStrokeEnd=@"shape_key_stroke_end";
NSString * const kShapeKeyLineWidth=@"shape_key_line_width";
NSString * const kShapeKeyFillColor=@"shape_key_fill_color";
NSString * const kShapeKeyStrokeColor=@"shape_key_stroke_color";
NSString * const kShapeKeyPosition=@"shape_key_position";
NSString * const kShapeKeyRotationX=@"shape_key_rotation_x";
NSString * const kShapeKeyRotationY=@"shape_key_rotation_y";
NSString * const kShapeKeyRotationZ=@"shape_key_rotation_z";
NSString * const kShapeKeyScaleX=@"shape_key_scale_x";
NSString * const kShapeKeyScaleY=@"shape_key_scale_y";
NSString * const kShapeKeyScaleZ=@"shape_key_scale_z";
NSString * const kShapeKeyScaleXY=@"shape_key_scale_xy";
NSString * const kShapeKey3DTransform=@"shape_key_3d_transform";
NSString * const kShapeKeyTransition=@"shape_key_transition";
NSString * const kShapeKeyOpacity=@"shape_key_opacity";
NSString * const kShapeKeyName=@"shape_key_name";
NSString * const kShapeDefaultName=@"shape_default_name";

NSString * const kShapeModeStrokeStart=@"shape_mode_stroke_start";
NSString * const kShapeModeStrokeEnd=@"shape_mode_stroke_end";
NSString * const kShapeModeLineWidth=@"shape_mode_line_width";
NSString * const kShapeModeFillColor=@"shape_mode_fill_color";
NSString * const kShapeModeStrokeColor=@"shape_mode_stroke_color";
NSString * const kShapeModePosition=@"shape_mode_position";
NSString * const kShapeModeRotationX=@"shape_mode_rotation_x";
NSString * const kShapeModeRotationY=@"shape_mode_rotation_y";
NSString * const kShapeModeRotationZ=@"shape_mode_rotation_z";
NSString * const kShapeModeScaleX=@"shape_mode_scale_x";
NSString * const kShapeModeScaleY=@"shape_mode_scale_y";
NSString * const kShapeModeScaleZ=@"shape_mode_scale_z";
NSString * const kShapeModeScaleXY=@"shape_mode_scale_xy";
NSString * const kShapeMode3DTransform=@"shape_mode_3d_transform";
NSString * const kShapeModeOpacity=@"shape_mode_opacity";

NSString * const kShapeTypeBasic=@"shape_type_basic";
NSString * const kShapeTypeKeyFrame=@"shape_type_key_frame";
NSString * const kShapeTypePath=@"shape_type_path";
NSString * const kShapeTypeGroup=@"shape_type_group";
NSString * const kShapeTypeTransition=@"shape_type_transition";

NSString * const kShapeAnimationHeartbeat=@"shape_animation_heartbeat";
NSString * const kShapeAnimationBlink=@"shape_animation_blink";
NSString * const kShapeAnimationRotate=@"shape_animation_rotate";
NSString * const kShapeAnimationFlip=@"shape_animation_flip";
NSString * const kShapeAnimationShake=@"shape_animation_shake";
NSString * const kShapeAnimationShock=@"shape_animation_shock";
NSString * const kShapeAnimationDither=@"sprite_animation_dither";
NSString * const kShapeAnimationDisappear=@"shape_animation_disappear";
NSString * const kShapeAnimationAppear=@"shape_animation_appear";
NSString * const kShapeAnimationScaledDisappear=@"shape_animation_scaled_disappear";
NSString * const kShapeAnimationScaledAppear=@"shape_animation_scaled_appear";
NSString * const kShapeAnimationZoomIn=@"shape_animation_zoom_in";
NSString * const kShapeAnimationZoomOut=@"shape_animation_zoom_out";
NSString * const kShapeAnimationRoll=@"shape_animation_roll";
NSString * const kShapeAnimationRollIn=@"shape_animation_roll_in";
NSString * const kShapeAnimationRollOut=@"shape_animation_roll_out";
NSString * const kShapeAnimationRotateIn=@"shape_animation_rotate_in";
NSString * const kShapeAnimationRotateOut=@"shape_animation_rotate_out";
NSString * const kShapeAnimationDrop=@"shape_animation_drop";
NSString * const kShapeAnimationFloat=@"shape_animation_float";
NSString * const kShapeAnimationMoveQuickly=@"shape_animation_move_quickly";
NSString * const kShapeAnimationMove=@"shape_animation_move";
NSString * const kShapeAnimationComeIn=@"shape_animation_come_in";
NSString * const kShapeAnimationGoOut=@"shape_animation_go_out";
NSString * const kShapeAnimationDraw=@"shape_animation_draw";
NSString * const kShapeAnimationErase=@"shape_animation_erase";
NSString * const kShapeAnimationShowPath=@"shape_animation_show_path";
NSString * const kShapeAnimationStrokeRange=@"shape_animation_stroke_range";
NSString * const kShapeAnimationJump=@"shape_animation_jump";



@implementation KATShape

+ (KATShape *)shape
{
    KATShape *shape=[[[self alloc] init] autorelease];
    
    return shape;
}


//初始化精灵
- (instancetype)init
{
    self=[super init];
    
    self.animating=NO;
    self.restoreAfterAnimating=NO;
    self.keepStateOnStop=YES;
    self.animationStoped=NO;
    self.angleX=0.0f;
    self.angleY=0.0f;
    self.angleZ=0.0f;
    self.sizeX=1.0f;
    self.sizeY=1.0f;
    self.sizeZ=1.0f;
    self.sizeXY=1.0f;
    self.perspective=-0.002;
    self.animation=nil;
    self.shapeDelegate=nil;
    self.animationCalculationMode=kCAAnimationLinear;
    self.animationFillMode=kCAFillModeRemoved;
    self.animationRotateMode=nil;
    self.animationTimingMode=kCAMediaTimingFunctionDefault;
    self.animationQueue=[KATQueue queueWithCapacity:16];
    
    return self;
}


//获取当前展示图层
- (CAShapeLayer *)presentLayer
{
    CAShapeLayer *present=(CAShapeLayer *)self.presentationLayer;
    
    if(present)
    {
        return present;
    }
    else
    {
        return self;
    }
}


#pragma -mark 获取动画

//获取描绘起始点动画(起始结束值在0~1之间)
- (CABasicAnimation *)animationStrokeStartToValue:(float)value withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"strokeStart"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.strokeStart];
    animation.toValue=[NSNumber numberWithFloat:value];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeStrokeStart forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:value] forKey:kShapeKeyStrokeStart];//值
    
    return animation;
}


//获取描绘结束点动画(起始结束值在0~1之间)
- (CABasicAnimation *)animationStrokeEndToValue:(float)value withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.strokeEnd];
    animation.toValue=[NSNumber numberWithFloat:value];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeStrokeEnd forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:value] forKey:kShapeKeyStrokeEnd];//值
    
    return animation;
}


//获取线宽动画
- (CABasicAnimation *)animationLineWidth:(float)width withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"lineWidth"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.lineWidth];
    animation.toValue=[NSNumber numberWithFloat:width];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeLineWidth forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:width] forKey:kShapeKeyLineWidth];//值
    
    return animation;
}


//获取填充色动画
- (CABasicAnimation *)animationFillColor:(UIColor *)color withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"fillColor"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=(__bridge id)self.fillColor;
    animation.toValue=(__bridge id)color.CGColor;
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=NO;//是否回到原来的位置(颜色恢复原来则不会有动画)
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeFillColor forKey:kShapeKeyMode];//模式
    [animation setValue:color forKey:kShapeKeyFillColor];//值
    
    return animation;
}


///获取描绘色动画
- (CABasicAnimation *)animationStrokeColor:(UIColor *)color withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"strokeColor"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=(__bridge id)self.strokeColor;
    animation.toValue=(__bridge id)color.CGColor;
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=NO;//是否回到原来的位置(颜色恢复原来则不会有动画)
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeStrokeColor forKey:kShapeKeyMode];//模式
    [animation setValue:color forKey:kShapeKeyStrokeColor];//值
    
    return animation;
}


//获取位移动画
- (CABasicAnimation *)animationToPosition:(CGPoint)position withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"position"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSValue valueWithCGPoint:self.position];
    animation.toValue=[NSValue valueWithCGPoint:position];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModePosition forKey:kShapeKeyMode];//模式
    [animation setValue:[NSValue valueWithCGPoint:position] forKey:kShapeKeyPosition];//值
    
    return animation;
}


//获取不透明度动画
- (CABasicAnimation *)animationToOpacity:(float)opacity withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.opacity];
    animation.toValue=[NSNumber numberWithFloat:opacity];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeOpacity forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:opacity] forKey:kShapeKeyOpacity];//值
    
    
    return animation;
}


//获取X轴旋转动画
- (CABasicAnimation *)animationRotationXToAngle:(float)angle withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.angleX];
    animation.toValue=[NSNumber numberWithFloat:angle];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeRotationX forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:angle] forKey:kShapeKeyRotationX];//值
    [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.transform,angle-self.angleX, 1.0, 0.0, 0.0)] forKey:kShapeKey3DTransform];//值
    
    
    return animation;
}


//获取Y轴旋转动画
- (CABasicAnimation *)animationRotationYToAngle:(float)angle withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.angleY];
    animation.toValue=[NSNumber numberWithFloat:angle];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeRotationY forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:angle] forKey:kShapeKeyRotationY];//值
    [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.transform,angle-self.angleY, 0.0, 1.0, 0.0)] forKey:kShapeKey3DTransform];//值
    
    
    return animation;
}


//获取Z轴旋转动画
- (CABasicAnimation *)animationRotationZToAngle:(float)angle withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.angleZ];
    animation.toValue=[NSNumber numberWithFloat:angle];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeRotationZ forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:angle] forKey:kShapeKeyRotationZ];//值
    [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.transform,angle-self.angleZ, 0.0, 0.0, 1.0)] forKey:kShapeKey3DTransform];//值
    
    
    return animation;
}


//获取X轴缩放动画
- (CABasicAnimation *)animationScaleXToSize:(float)size withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.sizeX];
    animation.toValue=[NSNumber numberWithFloat:size];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeScaleX forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:size] forKey:kShapeKeyScaleX];//值
    
    if(self.sizeX!=0.0)
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.transform, size/self.sizeX, 1.0, 1.0)] forKey:kShapeKey3DTransform];//值
    }
    else
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(size, 1.0, 1.0)] forKey:kShapeKey3DTransform];//值
    }
    
    
    return animation;
}


//获取Y轴缩放动画
- (CABasicAnimation *)animationScaleYToSize:(float)size withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.sizeY];
    animation.toValue=[NSNumber numberWithFloat:size];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeScaleY forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:size] forKey:kShapeKeyScaleY];//值
    
    if(self.sizeY!=0.0)
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.transform, 1.0, size/self.sizeY, 1.0)] forKey:kShapeKey3DTransform];//值
    }
    else
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, size, 1.0)] forKey:kShapeKey3DTransform];//值
    }
    
    
    return animation;
}


//获取Z轴缩放动画
- (CABasicAnimation *)animationScaleZToSize:(float)size withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale.z"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.sizeZ];
    animation.toValue=[NSNumber numberWithFloat:size];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeScaleZ forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:size] forKey:kShapeKeyScaleZ];//值
    
    if(self.sizeZ!=0.0)
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.transform, 1.0, 1.0, size/self.sizeZ)] forKey:kShapeKey3DTransform];//值
    }
    else
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, size)] forKey:kShapeKey3DTransform];//值
    }
    
    
    return animation;
}


//获取XY轴(等比例)缩放动画
- (CABasicAnimation *)animationScaleXYToSize:(float)size withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.sizeXY];
    animation.toValue=[NSNumber numberWithFloat:size];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeBasic forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModeScaleXY forKey:kShapeKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:size] forKey:kShapeKeyScaleXY];//值
    
    if(self.sizeXY!=0.0)
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.transform, size/self.sizeXY*self.sizeY/self.sizeXY, size/self.sizeXY*self.sizeX/self.sizeXY, 1.0)] forKey:kShapeKey3DTransform];//值
    }
    else
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(size, size, 1.0)] forKey:kShapeKey3DTransform];//值
    }
    
    return animation;
}


//获取路径动画(特殊的位移关键帧动画，优先级高于位移关键帧)
- (CAKeyframeAnimation *)animationWithPath:(UIBezierPath *)path duration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    if(!path)
    {
        return nil;
    }
    
    //创建关键帧动画并设置动画属性
    CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //设置动画路径
    animation.path=path.CGPath;
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    animation.calculationMode=self.animationCalculationMode;//计算模式
    animation.fillMode=self.animationFillMode;//填充模式
    animation.rotationMode=self.animationRotateMode;//旋转模式
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypePath forKey:kShapeKeyType];//类型
    [animation setValue:kShapeModePosition forKey:kShapeKeyMode];//模式
    [animation setValue:[NSValue valueWithCGPoint:path.currentPoint] forKey:kShapeKeyPosition];//值
    
    
    return animation;
}


//获取关键帧动画
- (CAKeyframeAnimation *)animationWithMode:(int)mode keyValues:(NSArray *)values keyTimes:(NSArray *)times duration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    if(!values)
    {
        return nil;
    }
    
    //创建关键帧动画并设置动画属性
    CAKeyframeAnimation *animation=nil;
    
    switch(mode)
    {
        case SHAPE_MODE_STROKE_START://描绘起始点模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"strokeStart"];
            
            //设置携带值
            [animation setValue:kShapeModeStrokeStart forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyStrokeStart];//值
            
            break;
            
            
        case SHAPE_MODE_STROKE_END://描绘结束点模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"strokeEnd"];
            
            //设置携带值
            [animation setValue:kShapeModeStrokeEnd forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyStrokeEnd];//值
            
            break;
            
            
        case SHAPE_MODE_LINE_WIDTH://线宽模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"lineWidth"];
            
            //设置携带值
            [animation setValue:kShapeModeLineWidth forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyLineWidth];//值
            
            break;
            
            
        case SHAPE_MODE_FILL_COLOR://填充色模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"fillColor"];
            
            //设置携带值
            [animation setValue:kShapeModeFillColor forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyFillColor];//值
            
            //颜色对象转化
            NSMutableArray *fillColors=[NSMutableArray array];
            
            for(UIColor *color in values)
            {
                if([color isKindOfClass:[UIColor class]])
                {
                    [fillColors addObject:(__bridge id)color.CGColor];
                }
            }
            
            values=fillColors;
            
            break;
            
            
        case SHAPE_MODE_STROKE_COLOR://绘制色模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"strokeColor"];
            
            //设置携带值
            [animation setValue:kShapeModeStrokeColor forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyStrokeColor];//值
            
            //颜色对象转化
            NSMutableArray *strokeColors=[NSMutableArray array];
            
            for(UIColor *color in values)
            {
                if([color isKindOfClass:[UIColor class]])
                {
                    [strokeColors addObject:(__bridge id)color.CGColor];
                }
            }
            
            values=strokeColors;
            
            break;
            
            
        case SHAPE_MODE_POSITION://位移模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
            
            //设置携带值
            [animation setValue:kShapeModePosition forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyPosition];//值
            
            break;
            
            
        case SHAPE_MODE_OPACITY://透明模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"opacity"];
            
            //设置携带值
            [animation setValue:kShapeModeOpacity forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyOpacity];//值
            
            break;
            
            
        case SHAPE_MODE_ROTATION_X://X轴旋转模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.x"];
            
            //设置携带值
            [animation setValue:kShapeModeRotationX forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyRotationX];//值
            [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.transform,[[values lastObject] floatValue]-self.angleX, 1.0, 0.0, 0.0)] forKey:kShapeKey3DTransform];//值
            
            break;
            
            
        case SHAPE_MODE_ROTATION_Y://Y轴旋转模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.y"];
            
            //设置携带值
            [animation setValue:kShapeModeRotationY forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyRotationY];//值
            [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.transform,[[values lastObject] floatValue]-self.angleY, 0.0, 1.0, 0.0)] forKey:kShapeKey3DTransform];//值
            
            break;
            
            
        case SHAPE_MODE_ROTATION_Z://Z轴旋转模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
            
            //设置携带值
            [animation setValue:kShapeModeRotationZ forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyRotationZ];//值
            [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.transform,[[values lastObject] floatValue]-self.angleZ, 0.0, 0.0, 1.0)] forKey:kShapeKey3DTransform];//值
            
            break;
            
            
        case SHAPE_MODE_SCALE_X://X轴缩放模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
            
            //设置携带值
            [animation setValue:kShapeModeScaleX forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyScaleX];//值
            
            if(self.sizeX!=0.0)
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.transform, [[values lastObject] floatValue]/self.sizeX, 1.0, 1.0)] forKey:kShapeKey3DTransform];//值
            }
            else
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale([[values lastObject] floatValue], 1.0, 1.0)] forKey:kShapeKey3DTransform];//值
            }
            
            break;
            
            
        case SHAPE_MODE_SCALE_Y://Y轴缩放模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
            
            //设置携带值
            [animation setValue:kShapeModeScaleY forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyScaleY];//值
            
            if(self.sizeY!=0.0)
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.transform, 1.0, [[values lastObject] floatValue]/self.sizeY, 1.0)] forKey:kShapeKey3DTransform];//值
            }
            else
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, [[values lastObject] floatValue], 1.0)] forKey:kShapeKey3DTransform];//值
            }
            
            break;
            
            
        case SHAPE_MODE_SCALE_Z://Z轴缩放模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.scale.z"];
            
            //设置携带值
            [animation setValue:kShapeModeScaleZ forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyScaleZ];//值
            
            if(self.sizeZ!=0.0)
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.transform, 1.0, 1.0, [[values lastObject] floatValue]/self.sizeZ)] forKey:kShapeKey3DTransform];//值
            }
            else
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, [[values lastObject] floatValue])] forKey:kShapeKey3DTransform];//值
            }
            
            break;
            
            
        case SHAPE_MODE_SCALE_XY://XY轴缩放模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
            
            //设置携带值
            [animation setValue:kShapeModeScaleXY forKey:kShapeKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kShapeKeyScaleXY];//值
            
            if(self.sizeXY!=0.0)
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.transform, [[values lastObject] floatValue]/self.sizeXY*self.sizeY/self.sizeXY, [[values lastObject] floatValue]/self.sizeXY*self.sizeX/self.sizeXY, 1.0)] forKey:kShapeKey3DTransform];//值
            }
            else
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale([[values lastObject] floatValue], [[values lastObject] floatValue], 1.0)] forKey:kShapeKey3DTransform];//值
            }
            
            break;
            
            
        default:
            
            return nil;
    }
    
    
    //设置动画关键值和时间
    animation.values=values;
    
    if(times)
    {
        animation.keyTimes=times;
    }
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    animation.calculationMode=self.animationCalculationMode;//计算模式
    animation.fillMode=self.animationFillMode;//填充模式
    animation.rotationMode=self.animationRotateMode;//旋转模式
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kShapeTypeKeyFrame forKey:kShapeKeyType];//类型
    
    return animation;
}


//获取动画组
- (CAAnimationGroup *)animationWithGroup:(NSArray *)group duration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    if(!group)
    {
        return nil;
    }
    
    //创建动画组
    CAAnimationGroup *animation=[CAAnimationGroup animation];
    
    //设置携带值
    [animation setValue:kShapeTypeGroup forKey:kShapeKeyType];//类型
    
    CATransform3D transform=self.transform;//形变变量
    NSMutableArray *array=[[[NSMutableArray alloc] init] autorelease];//最终放入动画组的数组（过滤重复模式）
    
    for(int i=0;i<group.count;i++)
    {
        CAAnimation *an=[group objectAtIndex:i];
        
        if(an && [an isKindOfClass:[CAAnimation class]])//判断类型
        {
            an.beginTime=an.beginTime-CACurrentMediaTime();//动画时间调整
            
            NSString *mode=[an valueForKey:kShapeKeyMode];
            NSString *key=nil;
            
            
            if([kShapeModeStrokeStart isEqualToString:mode])//描绘起始点模式
            {
                key=kShapeKeyStrokeStart;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeStrokeEnd isEqualToString:mode])//描绘结束点模式
            {
                key=kShapeKeyStrokeEnd;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeLineWidth isEqualToString:mode])//线宽模式
            {
                key=kShapeKeyLineWidth;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeFillColor isEqualToString:mode])//填充色模式
            {
                key=kShapeKeyFillColor;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeStrokeColor isEqualToString:mode])//绘制色模式
            {
                key=kShapeKeyStrokeColor;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModePosition isEqualToString:mode])//位移模式
            {
                key=kShapeKeyPosition;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeOpacity isEqualToString:mode])//透明模式
            {
                key=kShapeKeyOpacity;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeRotationX isEqualToString:mode])//X旋转模式
            {
                key=kShapeKeyRotationX;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    transform=CATransform3DRotate(transform,[[an valueForKey:key] floatValue]-self.angleX, 1.0, 0.0, 0.0);//形变
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeRotationY isEqualToString:mode])//Y旋转模式
            {
                key=kShapeKeyRotationY;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    transform=CATransform3DRotate(transform,[[an valueForKey:key] floatValue]-self.angleY, 0.0, 1.0, 0.0);//形变
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeRotationZ isEqualToString:mode])//Y旋转模式
            {
                key=kShapeKeyRotationZ;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    transform=CATransform3DRotate(transform,[[an valueForKey:key] floatValue]-self.angleZ, 0.0, 0.0, 1.0);//形变
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeScaleX isEqualToString:mode])//X缩放模式
            {
                key=kShapeKeyScaleX;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    if(self.sizeX!=0.0)
                    {
                        transform=CATransform3DScale(transform, [[an valueForKey:key] floatValue]/self.sizeX, 1.0, 1.0);//形变
                    }
                    else
                    {
                        transform=CATransform3DMakeScale([[an valueForKey:key] floatValue], 1.0, 1.0);//形变
                    }
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeScaleY isEqualToString:mode])//Y缩放模式
            {
                key=kShapeKeyScaleY;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    if(self.sizeY!=0.0)
                    {
                        transform=CATransform3DScale(transform, 1.0, [[an valueForKey:key] floatValue]/self.sizeY, 1.0);//形变
                    }
                    else
                    {
                        transform=CATransform3DMakeScale(1.0, [[an valueForKey:key] floatValue], 1.0);//形变
                    }
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeScaleZ isEqualToString:mode])//Z缩放模式
            {
                key=kShapeKeyScaleZ;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    if(self.sizeZ!=0.0)
                    {
                        transform=CATransform3DScale(transform, 1.0, 1.0, [[an valueForKey:key] floatValue]/self.sizeZ);//形变
                    }
                    else
                    {
                        transform=CATransform3DMakeScale(1.0, 1.0, [[an valueForKey:key] floatValue]);//形变
                    }
                    
                    [array addObject:an];
                }
            }
            else if([kShapeModeScaleXY isEqualToString:mode])//XY缩放模式
            {
                key=kShapeKeyScaleXY;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    if(self.sizeXY!=0.0)
                    {
                        transform=CATransform3DScale(transform, [[an valueForKey:key] floatValue]/self.sizeXY*self.sizeY/self.sizeXY, [[an valueForKey:key] floatValue]/self.sizeXY*self.sizeX/self.sizeXY, 1.0);//形变
                    }
                    else
                    {
                        transform=CATransform3DMakeScale([[an valueForKey:key] floatValue], [[an valueForKey:key] floatValue], 1.0);//形变
                    }
                    
                    [array addObject:an];
                }
            }
        }
    }
    
    
    [animation setValue:[NSValue valueWithCATransform3D:transform] forKey:kShapeKey3DTransform];//设置形变值
    
    animation.animations=array;//设置动画组
    
    //设置动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    animation.fillMode=self.animationFillMode;//填充模式
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    return animation;
}


#pragma -mark 动画执行

//开始动画(设置动画名字)
- (void)startAnimation:(CAAnimation *)animation withName:(NSString *)name andDelay:(double)delay;
{
    if(animation && name)
    {
        //防止循环引用
        animation=[[animation copy] autorelease];
        
        animation.delegate=self;
        animation.beginTime=CACurrentMediaTime()+delay;//设置开始时间
        
        [animation setValue:name forKey:kShapeKeyName];//设置动画名称
        
        if(!_animating)//没有动画执行，则直接执行动画
        {
            _animating=YES;
            
            [self addAnimation:animation forKey:name];
        }
        else//有动画在执行中，则放入队列
        {
            [self.animationQueue put:animation];
        }
    }
}


//开始动画(默认动画名字)
- (void)startAnimation:(CAAnimation *)animation andDelay:(double)delay;
{
    if(animation)
    {
        if([animation valueForKey:kShapeKeyName])//本身带名字的（队列里的动画）
        {
            [self startAnimation:animation withName:[animation valueForKey:kShapeKeyName] andDelay:0];
        }
        else
        {
            [self startAnimation:animation withName:kShapeDefaultName andDelay:0];
        }
    }
    
}


//暂停动画
- (void)pauseAnimation
{
    if(self.speed>0)
    {
        //取得指定图层动画的媒体时间，后面参数用于指定子图层，这里不需要
        CFTimeInterval interval=[self convertTime:CACurrentMediaTime() fromLayer:nil];
        
        //设置时间偏移量，保证暂停时停留在旋转的位置
        [self setTimeOffset:interval];
        
        //速度设置为0，暂停动画
        self.speed=0;
    }
}


//恢复动画
- (void)resumeAnimation
{
    if(self.speed<1.0)
    {
        //获得暂停的时间
        CFTimeInterval beginTime=CACurrentMediaTime()-self.timeOffset;
        
        //设置偏移量
        self.timeOffset=0;
        
        //设置开始时间
        self.beginTime=beginTime;
        
        //设置动画速度，开始运动
        self.speed=1.0;
    }
}




#pragma -mark 变形

//旋转
- (void)transformRotateToAngle:(float)angle
{
    self.transform=CATransform3DRotate(CATransform3DIdentity, angle, 0.0, 0.0, 1.0);
    
    //保存变换角度
    self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//旋转X轴（上下转）
- (void)transformRotateXToAngle:(float)angle
{
    CATransform3D transform=CATransform3DIdentity;
    
    transform.m34=_perspective;
    
    self.transform=CATransform3DRotate(transform, angle, 1.0, 0.0, 0.0);
    
    //保存变换角度
    self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//旋转Y轴（左右转）
- (void)transformRotateYToAngle:(float)angle
{
    CATransform3D transform=CATransform3DIdentity;
    
    transform.m34=_perspective;
    
    self.transform=CATransform3DRotate(transform, angle, 0.0, 1.0, 0.0);
    
    //保存变换角度
    self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//水平镜像
- (void)transformHorizontalMirror
{
    self.transform=CATransform3DRotate(CATransform3DIdentity, M_PI, 1.0, 0.0, 0.0);
    
    //保存变换角度
    self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//垂直镜像
- (void)transformVerticalMirror
{
    self.transform=CATransform3DRotate(CATransform3DIdentity, M_PI, 0.0, 1.0, 0.0);
    
    //保存变换角度
    self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//缩放(宽高)
- (void)transformScaleToWitdh:(float)width andHeight:(float)height
{
    self.transform=CATransform3DScale(CATransform3DIdentity, width, height, 1.0);
    
    //保存变换角度
    self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//复位
- (void)restoreShape
{
    //停止动画
    [self stopAnimation];
    
    self.transform=CATransform3DIdentity;
    self.sizeX=1.0f;
    self.sizeY=1.0f;
    self.sizeZ=1.0f;
    self.sizeXY=1.0f;
    self.angleX=0.0f;
    self.angleY=0.0f;
    self.angleZ=0.0f;
    
    self.opacity=1.0f;
}



#pragma -mark 常用动画

//心跳(等比缩放)(参数小于0则用默认值)
- (void)heartbeatWithScale:(float)scale duration:(double)duration repeat:(float)repeat
{
    if(scale<0)
    {
        scale=1.16;
    }
    
    if(duration<=0)
    {
        duration=0.6;
    }
    
    [self startAnimation:[self animationScaleXYToSize:scale withDuration:duration repeat:repeat autoreverses:YES delay:0.0] withName:kShapeAnimationHeartbeat andDelay:0];
}


//闪烁(透明变化)(参数小于0则用默认值)
- (void)blinkWithAlpha:(float)alpha duration:(double)duration repeat:(float)repeat
{
    if(alpha<0)
    {
        alpha=0.2;
    }
    
    if(duration<=0)
    {
        duration=0.3;
    }
    
    [self startAnimation:[self animationToOpacity:alpha withDuration:duration repeat:repeat autoreverses:YES delay:0.0] withName:kShapeAnimationBlink andDelay:0];
}


//转动(Z轴旋转)(参数小于0则用默认值)
- (void)rotateWithDuration:(double)duration repeat:(float)repeat clockwise:(BOOL)clockwise
{
    if(duration<=0)
    {
        duration=1.6;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationRotationZToAngle:clockwise?(_angleZ+M_PI*2):(_angleZ-M_PI*2) withDuration:duration repeat:repeat autoreverses:NO delay:0] withName:kShapeAnimationRotate andDelay:0];
}


//翻转(XY轴旋转)(参数小于0则用默认值)
- (void)flipWithDuration:(double)duration repeat:(float)repeat clockwise:(BOOL)clockwise vertical:(BOOL)vertical
{
    if(duration<=0)
    {
        duration=1.6;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    if(vertical)
    {
        [self startAnimation:[self animationRotationXToAngle:clockwise?(_angleZ+M_PI*2):(_angleZ-M_PI*2) withDuration:duration repeat:repeat autoreverses:NO delay:0] withName:kShapeAnimationRotate andDelay:0];
    }
    else
    {
        [self startAnimation:[self animationRotationYToAngle:clockwise?(_angleZ+M_PI*2):(_angleZ-M_PI*2) withDuration:duration repeat:repeat autoreverses:NO delay:0] withName:kShapeAnimationRotate andDelay:0];
    }
}


//摇晃(左右)(参数小于0则用默认值)
- (void)shakeWithDegree:(float)degree duration:(double)duration repeat:(float)repeat
{
    if(degree<0)
    {
        degree=2.0;
    }
    
    if(duration<0)
    {
        duration=0.08;
    }
    
    [self startAnimation:[self animationWithMode:SHAPE_MODE_POSITION keyValues:@[[NSValue valueWithCGPoint:CGPointMake(self.position.x-degree, self.position.y)],[NSValue valueWithCGPoint:self.position],[NSValue valueWithCGPoint:CGPointMake(self.position.x+degree, self.position.y)],[NSValue valueWithCGPoint:self.position]] keyTimes:@[@(0.25),@(0.5),@(0.75),@(1.0)] duration:duration repeat:repeat autoreverses:NO delay:0] withName:kShapeAnimationShake andDelay:0];
}


//震动(上下)(参数小于0则用默认值)
- (void)shockWithDegree:(float)degree duration:(double)duration repeat:(float)repeat
{
    if(degree<0)
    {
        degree=2.0;
    }
    
    if(duration<0)
    {
        duration=0.08;
    }
    
    [self startAnimation:[self animationWithMode:SHAPE_MODE_POSITION keyValues:@[[NSValue valueWithCGPoint:CGPointMake(self.position.x, self.position.y+degree)],[NSValue valueWithCGPoint:self.position],[NSValue valueWithCGPoint:CGPointMake(self.position.x, self.position.y-degree)],[NSValue valueWithCGPoint:self.position]] keyTimes:@[@(0.25),@(0.5),@(0.75),@(1.0)] duration:duration repeat:repeat autoreverses:NO delay:0] withName:kShapeAnimationShock andDelay:0];
}


//抖动(旋转)(参数小于0则用默认值)
- (void)ditherWithDegree:(float)degree duration:(double)duration repeat:(float)repeat
{
    if(degree<0)
    {
        degree=2.0;
    }
    
    if(duration<0)
    {
        duration=0.1;
    }
    
    
    [self startAnimation:[self animationWithMode:SHAPE_MODE_ROTATION_Z keyValues:@[@(self.angleZ-M_PI/180.0*degree),@(self.angleZ),@(self.angleZ+M_PI/180.0*degree),@(self.angleZ)] keyTimes:@[@(0.25),@(0.5),@(0.75),@(1.0)] duration:duration repeat:repeat autoreverses:NO delay:0] withName:kShapeAnimationDither andDelay:0];
}


//消失(透明变化)
- (void)disappearWithDuration:(double)duration
{
    if(duration<0)
    {
        duration=0.6f;
    }
    
    [self startAnimation:[self animationToOpacity:0.0 withDuration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationDisappear andDelay:0];
}


//出现(透明变化)
- (void)appearWithDuration:(double)duration
{
    if(duration<0)
    {
        duration=0.6f;
    }
    
    [self startAnimation:[self animationToOpacity:1.0 withDuration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationAppear andDelay:0];
}


//消失(缩放变化)
- (void)scaledDisappearWithDuration:(double)duration
{
    if(duration<0)
    {
        duration=0.6f;
    }
    
    [self startAnimation:[self animationScaleXYToSize:0.0 withDuration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationScaledDisappear andDelay:0];
}


//出现(缩放变化)
- (void)scaledAppearWithDuration:(double)duration
{
    if(duration<0)
    {
        duration=0.6f;
    }
    
    if(_sizeXY==0)
    {
        [self restoreShape];
    }
    
    [self startAnimation:[self animationScaleXYToSize:1.0 withDuration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationScaledAppear andDelay:0];
}


//焦点出(缩放、透明变化)
- (void)zoomOutWithScale:(float)scale duration:(double)duration
{
    if(duration<0)
    {
        duration=1.38f;
    }
    
    if(scale<0)
    {
        scale=1.62;
    }
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithMode:SHAPE_MODE_SCALE_XY keyValues:@[@(1.0),@(scale)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SHAPE_MODE_OPACITY keyValues:@[@(1.0),@(0.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationZoomOut andDelay:0];
}


//焦点进(缩放、透明变化)
- (void)zoomInWithScale:(float)scale duration:(double)duration
{
    if(duration<0)
    {
        duration=1.38f;
    }
    
    if(scale<0)
    {
        scale=1.62;
    }
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithMode:SHAPE_MODE_SCALE_XY keyValues:@[@(scale),@(1.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SHAPE_MODE_OPACITY keyValues:@[@(0.0),@(1.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationZoomIn andDelay:0];
}


//滚动(旋转、位移变化)
- (void)rollToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<0)
    {
        duration=1.0;
    }
    
    float radius=(self.bounds.size.width+self.bounds.size.height)/2.0/2.0;//半径
    
    if(radius<=0.0)
    {
        radius=1.0;
    }
    
    double distance=[KATMath distanceWithPointA:position andPointB:self.position];//距离
    BOOL clockwise=position.x>=self.position.x?YES:NO;
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationRotationZToAngle:self.angleZ+(clockwise?(distance/radius):-(distance/radius)) withDuration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationRoll andDelay:0];
}


//滚进(旋转、位移、透明变化)
- (void)rollInToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<0)
    {
        duration=1.0;
    }
    
    float radius=(self.bounds.size.width+self.bounds.size.height)/2.0/2.0;//半径
    
    if(radius<=0.0)
    {
        radius=1.0;
    }
    
    double distance=[KATMath distanceWithPointA:position andPointB:self.position];//距离
    BOOL clockwise=position.x>=self.position.x?YES:NO;
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationRotationZToAngle:self.angleZ+(clockwise?(distance/radius):-(distance/radius)) withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SHAPE_MODE_OPACITY keyValues:@[@(0.0),@(1.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationRollIn andDelay:0];
}


//滚出(旋转、位移、透明变化)
- (void)rollOutToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<0)
    {
        duration=1.0;
    }
    
    float radius=(self.bounds.size.width+self.bounds.size.height)/2.0/2.0;//半径
    
    if(radius<=0.0)
    {
        radius=1.0;
    }
    
    double distance=[KATMath distanceWithPointA:position andPointB:self.position];//距离
    BOOL clockwise=position.x>=self.position.x?YES:NO;
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationRotationZToAngle:self.angleZ+(clockwise?(distance/radius):-(distance/radius)) withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SHAPE_MODE_OPACITY keyValues:@[@(1.0),@(0.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationRollOut andDelay:0];
}


//转进(旋转、缩放、透明变化)
- (void)rotateInWithCircles:(int)circles duration:(double)duration
{
    if(duration<=0)
    {
        duration=1.38;
    }
    
    if(circles<=0)
    {
        circles=3;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithMode:SHAPE_MODE_SCALE_XY keyValues:@[@(0.0),@(1.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SHAPE_MODE_OPACITY keyValues:@[@(0.0),@(1.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationRotationZToAngle:self.angleZ+M_PI*2 withDuration:duration/circles repeat:circles autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationRotateIn andDelay:0];
}


//转出(旋转、缩放、透明变化)
- (void)rotateOutWithCircles:(int)circles duration:(double)duration
{
    if(duration<=0)
    {
        duration=1.38;
    }
    
    if(circles<=0)
    {
        circles=3;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithMode:SHAPE_MODE_SCALE_XY keyValues:@[@(1.0),@(0.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SHAPE_MODE_OPACITY keyValues:@[@(1.0),@(0.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationRotationZToAngle:self.angleZ-M_PI*2 withDuration:duration/circles repeat:circles autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kShapeAnimationRotateOut andDelay:0];
}


//掉落
- (void)dropToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<=0)
    {
        duration=sqrt(fabs(position.y-self.position.y)*2/9.8)*0.138;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionEaseIn;
    
    //路径
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path moveToPoint:self.position];
    [path addQuadCurveToPoint:position controlPoint:CGPointMake(position.x, self.position.y)];
    
    //角度
    float angle=atan2(position.x-self.position.x, position.y-self.position.y);
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithPath:path duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SHAPE_MODE_ROTATION_Z keyValues:@[@(self.angleZ-angle),@(self.angleZ)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kShapeAnimationDrop andDelay:0];
}


//飘落
- (void)floatToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<=0)
    {
        duration=fabs(position.y-self.position.y)*0.012;
    }
    
    //距离
    double distance=[KATMath distanceWithPointA:position andPointB:self.position];
    
    //角度
    float angle=atan2(position.x-self.position.x, position.y-self.position.y);
    
    //路径
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path moveToPoint:self.position];
    [path addCurveToPoint:position controlPoint1:CGPointMake((position.x+self.position.x)/2.0-(distance/2.0/tan(M_PI/6.0)*sin(M_PI/2.0-angle)), (position.y+self.position.y)/2.0-(distance/2.0/tan(M_PI/2.0-M_PI/6.0)*cos(M_PI/2.0-angle))) controlPoint2:CGPointMake((position.x+self.position.x)/2.0+(distance/2.0/tan(M_PI/6.0)*sin(M_PI/2.0+angle)), (position.y+self.position.y)/2.0+(distance/2.0/tan(M_PI/2.0-M_PI/6.0)*cos(M_PI/2.0-angle)))];
    
    //调整z轴
    self.zPosition=self.zPosition+self.bounds.size.height;
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithPath:path duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SHAPE_MODE_ROTATION_Z keyValues:@[@(self.angleZ-angle),@(self.angleZ-angle+M_PI),@(self.angleZ-angle)] keyTimes:@[@(0.0),@(0.5),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SHAPE_MODE_ROTATION_X keyValues:@[@(self.angleX),@(self.angleX+M_PI),@(self.angleX)] keyTimes:@[@(0.0),@(0.5),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kShapeAnimationFloat andDelay:0];
}


//快速移动
- (void)moveQuicklyToPosition:(CGPoint)position withDegree:(float)degree duration:(double)duration
{
    if(duration<=0)
    {
        duration=sqrt([KATMath distanceWithPointA:position andPointB:self.position])*0.01;
    }
    
    if(degree<0)
    {
        degree=[KATMath distanceWithPointA:position andPointB:self.position]*0.06;
    }
    
    //方向
    float directionX=0.0f;
    float directionY=0.0f;
    
    if(position.x>self.position.x)
    {
        directionX=1.0;
    }
    else if(position.x<self.position.x)
    {
        directionX=-1.0;
    }
    
    if(position.y>self.position.y)
    {
        directionY=1.0;
    }
    else if(position.y<self.position.y)
    {
        directionY=-1.0;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionEaseInEaseOut;
    
    [self startAnimation:[self animationWithMode:SHAPE_MODE_POSITION keyValues:@[[NSValue valueWithCGPoint:self.position],[NSValue valueWithCGPoint:CGPointMake(position.x+directionX*degree, position.y+directionY*degree)],[NSValue valueWithCGPoint:CGPointMake(position.x-directionX*degree*0.4, position.y-directionY*degree*0.4)],[NSValue valueWithCGPoint:CGPointMake(position.x+directionX*degree*0.2, position.y+directionY*degree*0.2)],[NSValue valueWithCGPoint:position]] keyTimes:@[@(0.0),@(0.55),@(0.7),@(0.85),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kShapeAnimationMoveQuickly andDelay:0];
}


//移动
- (void)moveToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<=0)
    {
        duration=sqrt([KATMath distanceWithPointA:position andPointB:self.position])*0.012;
    }
    
    //    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0] withName:kShapeAnimationMove andDelay:0];
}


//移动出现
- (void)comeInToPosition:(CGPoint)position withDuration:(double)duration
{
    [self startAnimation:[self animationWithGroup:@[[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationToOpacity:1.0 withDuration:duration repeat:1.0 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kShapeAnimationComeIn andDelay:0];
}


//移动消失
- (void)goOutToPosition:(CGPoint)position withDuration:(double)duration
{
    [self startAnimation:[self animationWithGroup:@[[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationToOpacity:0.0 withDuration:duration repeat:1.0 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kShapeAnimationGoOut andDelay:0];
}


//绘制
- (void)drawShapeReverse:(BOOL)reverse withDuration:(double)duration
{
    if(duration<=0)
    {
        duration=2.4;
    }
    
    if(reverse)
    {
        [self startAnimation:[self animationStrokeStartToValue:0.0 withDuration:duration repeat:1 autoreverses:NO delay:0] andDelay:0];
    }
    else
    {
        [self startAnimation:[self animationStrokeEndToValue:1.0 withDuration:duration repeat:1 autoreverses:NO delay:0] andDelay:0];
    }
}


//擦除
- (void)eraseShapeReverse:(BOOL)reverse withDuration:(double)duration
{
    if(duration<=0)
    {
        duration=2.4;
    }
    
    if(reverse)
    {
        [self startAnimation:[self animationStrokeStartToValue:1.0 withDuration:duration repeat:1 autoreverses:NO delay:0] andDelay:0];
    }
    else
    {
        [self startAnimation:[self animationStrokeEndToValue:0.0 withDuration:duration repeat:1 autoreverses:NO delay:0] andDelay:0];
    }
}


//显示路径(范围为0~1)
- (void)showPathRange:(float)range reverse:(BOOL)reverse withDuration:(double)duration
{
    if(duration<=0)
    {
        duration=2.4;
    }
    
    if(range<=0.0 || range>=1.0)
    {
        range=0.01;
    }
    
    if(reverse)
    {
        self.strokeStart=1.0;
        self.strokeEnd=1.0+range;
        
        [self startAnimation:[self animationWithGroup:@[[self animationStrokeStartToValue:-range withDuration:duration repeat:1 autoreverses:NO delay:0],[self animationStrokeEndToValue:0.0 withDuration:duration repeat:1 autoreverses:NO delay:0]] duration:duration repeat:1 autoreverses:NO delay:0] andDelay:0];
    }
    else
    {
        self.strokeStart=-range;
        self.strokeEnd=0.0;
        
        [self startAnimation:[self animationWithGroup:@[[self animationStrokeStartToValue:1.0 withDuration:duration repeat:1 autoreverses:NO delay:0],[self animationStrokeEndToValue:1.0+range withDuration:duration repeat:1 autoreverses:NO delay:0]] duration:duration repeat:1 autoreverses:NO delay:0] andDelay:0];
    }
}


//绘制范围
- (void)strokeRangeWithStart:(float)start end:(float)end duration:(double)duration
{
    if(duration<=0)
    {
        duration=1.0;
    }
    
    if(start<0)
    {
        start=0;
    }
    
    if(end>1.0)
    {
        end=1.0;
    }
    
    [self startAnimation:[self animationWithGroup:@[[self animationStrokeStartToValue:start withDuration:duration repeat:1 autoreverses:NO delay:0],[self animationStrokeEndToValue:end withDuration:duration repeat:1 autoreverses:NO delay:0]] duration:duration repeat:1 autoreverses:NO delay:0] andDelay:0];
}


//跳跃
- (void)jumpToPosition:(CGPoint)position withHeight:(float)height andDuration:(double)duration
{
    if(height<=0)
    {
        height=fabs(position.x-self.position.x)/2.0;
    }
    
    if(duration<=0)
    {
        duration=sqrt([KATMath distanceWithPointA:position andPointB:self.position])*0.012+height*0.012+0.18;
    }
    
    //抛物线顶点式方程式y=a(x-h)^2+k (k为顶点高度)
    
    //转换坐标
    float x1=position.x-self.position.x;
    float y1=self.position.y-position.y;
    
    //判断高度是否为零或低于y2
    if(y1>height)
    {
        height=y1;
    }
    
    //计算
    float k=height;
    float h=x1/(sqrtf(1.0-y1/k)+1.0);
    
    //生成路径
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake(self.position.x, self.position.y)];
    [path addQuadCurveToPoint:CGPointMake(position.x, position.y) controlPoint:CGPointMake(self.position.x+h, self.position.y-k-(k-y1/2.0))];
    
    self.animationTimingMode=kCAMediaTimingFunctionEaseInEaseOut;
    
    [self startAnimation:[self animationWithPath:path duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kShapeAnimationJump andDelay:0];
}


//清除所有动画(包括当前动画和队列里的动画)
- (void)clearAnimations
{
    if(self.animationQueue)
    {
        [_animationQueue clear];
    }
    
    [self stopAnimation];
}


//停止并移除当前动画
- (void)stopAnimation
{
    //先取消暂停
    [self resumeAnimation];
    
    if(_keepStateOnStop && _animation)//停止时保持现场
    {
        CAShapeLayer *present=(CAShapeLayer *)self.presentationLayer;
        
        if(present)
        {
            if(_animation)
            {
                NSString *type=[_animation valueForKey:kShapeKeyType];//类型
                
                if([kShapeTypeBasic isEqualToString:type] || [kShapeTypeKeyFrame isEqualToString:type])//基础类型,关键帧类型
                {
                    NSString *mode=[_animation valueForKey:kShapeKeyMode];//模式
                    
                    if([kShapeModePosition isEqualToString:mode])//位移模式
                    {
                        self.position=present.position;
                    }
                    else if([kShapeModeRotationX isEqualToString:mode])//x轴旋转模式
                    {
                        self.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kShapeModeRotationY isEqualToString:mode])//y轴旋转模式
                    {
                        self.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kShapeModeRotationZ isEqualToString:mode])//z轴旋转模式
                    {
                        self.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kShapeModeScaleX isEqualToString:mode])//x轴缩放模式
                    {
                        self.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kShapeModeScaleY isEqualToString:mode])//y轴缩放模式
                    {
                        self.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kShapeModeScaleZ isEqualToString:mode])//z轴缩放模式
                    {
                        self.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kShapeModeScaleXY isEqualToString:mode])//xy轴缩放模式
                    {
                        self.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kShapeModeOpacity isEqualToString:mode])//不透明度模式
                    {
                        self.opacity=present.opacity;
                    }
                    else if([kShapeModeStrokeStart isEqualToString:mode])//绘制起始点模式
                    {
                        self.strokeStart=present.strokeStart;
                    }
                    else if([kShapeModeStrokeEnd isEqualToString:mode])//绘制结束点模式
                    {
                        self.strokeEnd=present.strokeEnd;
                    }
                    else if([kShapeModeLineWidth isEqualToString:mode])//线宽模式
                    {
                        self.lineWidth=present.lineWidth;
                    }
                }
                else if([kShapeTypePath isEqualToString:type])//路径类型
                {
                    NSString *mode=[_animation valueForKey:kShapeKeyMode];//模式
                    
                    if([kShapeModePosition isEqualToString:mode])//位移模式
                    {
                        self.position=present.position;
                        
                        
                        self.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                }
                else if([kShapeTypePath isEqualToString:type])//组类型
                {
                    self.position=present.position;
                    self.opacity=present.opacity;
                    self.strokeStart=present.strokeStart;
                    self.strokeEnd=present.strokeEnd;
                    self.lineWidth=present.lineWidth;
                    
                    self.transform=present.transform;
                    
                    //保存变换角度
                    self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                    self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                    self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                    
                    //保存变换尺寸
                    self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                    self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                    self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                    self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                }
            }
            
        }
    }
    
    //移除所有动画
    [self removeAllAnimations];
    
    _animating=NO;
}


//保持动画结束后状态
- (void)keepFinishState
{
    if(_animation)
    {
        NSString *type=[_animation valueForKey:kShapeKeyType];//类型
        
        if([kShapeTypeBasic isEqualToString:type] || [kShapeTypeKeyFrame isEqualToString:type])//基础类型,关键帧类型
        {
            NSString *mode=[_animation valueForKey:kShapeKeyMode];//模式
            
            if([kShapeModePosition isEqualToString:mode])//位移模式
            {
                NSValue *value=[_animation valueForKey:kShapeKeyPosition];
                
                if(value)
                {
                    self.position=[value CGPointValue];
                }
            }
            else if([kShapeModeRotationX isEqualToString:mode])//x轴旋转模式
            {
                NSValue *value=[_animation valueForKey:kShapeKey3DTransform];
                
                if(value)
                {
                    self.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kShapeModeRotationY isEqualToString:mode])//y轴旋转模式
            {
                NSValue *value=[_animation valueForKey:kShapeKey3DTransform];
                
                if(value)
                {
                    self.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kShapeModeRotationZ isEqualToString:mode])//z轴旋转模式
            {
                NSValue *value=[_animation valueForKey:kShapeKey3DTransform];
                
                if(value)
                {
                    self.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kShapeModeScaleX isEqualToString:mode])//x轴缩放模式
            {
                NSValue *value=[_animation valueForKey:kShapeKey3DTransform];
                
                if(value)
                {
                    self.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kShapeModeScaleY isEqualToString:mode])//y轴缩放模式
            {
                NSValue *value=[_animation valueForKey:kShapeKey3DTransform];
                
                if(value)
                {
                    self.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kShapeModeScaleZ isEqualToString:mode])//z轴缩放模式
            {
                NSValue *value=[_animation valueForKey:kShapeKey3DTransform];
                
                if(value)
                {
                    self.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kShapeModeScaleXY isEqualToString:mode])//xy轴缩放模式
            {
                NSValue *value=[_animation valueForKey:kShapeKey3DTransform];
                
                if(value)
                {
                    self.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kShapeModeOpacity isEqualToString:mode])//不透明度模式
            {
                NSNumber *value=[_animation valueForKey:kShapeKeyOpacity];
                
                if(value)
                {
                    self.opacity=[value floatValue];
                }
            }
            else if([kShapeModeStrokeStart isEqualToString:mode])//绘制起始点模式
            {
                NSNumber *value=[_animation valueForKey:kShapeKeyStrokeStart];
                
                if(value)
                {
                    self.strokeStart=[value floatValue];
                }
            }
            else if([kShapeModeStrokeEnd isEqualToString:mode])//绘制结束点模式
            {
                NSNumber *value=[_animation valueForKey:kShapeKeyStrokeEnd];
                
                if(value)
                {
                    self.strokeEnd=[value floatValue];
                }
            }
            else if([kShapeModeLineWidth isEqualToString:mode])//线宽模式
            {
                NSNumber *value=[_animation valueForKey:kShapeKeyLineWidth];
                
                if(value)
                {
                    self.lineWidth=[value floatValue];
                }
            }
            else if([kShapeModeFillColor isEqualToString:mode])//填充色模式
            {
                UIColor *value=[_animation valueForKey:kShapeKeyFillColor];
                
                if(value)
                {
                    self.fillColor=value.CGColor;
                }
            }
            else if([kShapeModeStrokeColor isEqualToString:mode])//绘制色模式
            {
                UIColor *value=[_animation valueForKey:kShapeKeyStrokeColor];
                
                if(value)
                {
                    self.strokeColor=value.CGColor;
                }
            }
        }
        else if([kShapeTypePath isEqualToString:type])//路径类型
        {
            NSString *mode=[_animation valueForKey:kShapeKeyMode];//模式
            
            if([kShapeModePosition isEqualToString:mode])//位移模式
            {
                NSValue *value=[_animation valueForKey:kShapeKeyPosition];
                
                if(value)
                {
                    self.position=[value CGPointValue];
                }
            }
        }
        else if([kShapeTypeGroup isEqualToString:type])//组类型
        {
            if([_animation valueForKey:kShapeKeyPosition])//包含位移值
            {
                NSValue *value=[_animation valueForKey:kShapeKeyPosition];
                
                if(value)
                {
                    self.position=[value CGPointValue];
                }
            }
            
            if([_animation valueForKey:kShapeKeyOpacity])//包含透明值
            {
                NSNumber *value=[_animation valueForKey:kShapeKeyOpacity];
                
                if(value)
                {
                    self.opacity=[value floatValue];
                }
            }
            
            if([_animation valueForKey:kShapeKeyStrokeStart])//包含绘制起始点值
            {
                NSNumber *value=[_animation valueForKey:kShapeKeyStrokeStart];
                
                if(value)
                {
                    self.strokeStart=[value floatValue];
                }
            }
            
            if([_animation valueForKey:kShapeKeyStrokeEnd])//包含绘制结束点值
            {
                NSNumber *value=[_animation valueForKey:kShapeKeyStrokeEnd];
                
                if(value)
                {
                    self.strokeEnd=[value floatValue];
                }
            }
            
            if([_animation valueForKey:kShapeKeyLineWidth])//包含线宽值
            {
                NSNumber *value=[_animation valueForKey:kShapeKeyLineWidth];
                
                if(value)
                {
                    self.lineWidth=[value floatValue];
                }
            }
            
            if([_animation valueForKey:kShapeKeyFillColor])//包含填充色值
            {
                UIColor *value=[_animation valueForKey:kShapeKeyFillColor];
                
                if(value)
                {
                    self.fillColor=value.CGColor;
                }
            }
            
            if([_animation valueForKey:kShapeKeyStrokeColor])//包含绘制色值
            {
                UIColor *value=[_animation valueForKey:kShapeKeyStrokeColor];
                
                if(value)
                {
                    self.strokeColor=value.CGColor;
                }
            }
            
            if([_animation valueForKey:kShapeKey3DTransform])//包含3d形变
            {
                NSValue *value=[_animation valueForKey:kShapeKey3DTransform];
                
                if(value)
                {
                    self.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            
        }
    }
}


#pragma -mark 代理

//动画开始
-(void)animationDidStart:(CAAnimation *)anim
{
    _animating=YES;
    _animationStoped=NO;
    self.animation=anim;
    
    if(!_restoreAfterAnimating && !anim.autoreverses)//非恢复原状
    {
        [self keepFinishState];
    }
    
    //回调
    if(_shapeDelegate && [_shapeDelegate respondsToSelector:@selector(shape:didStartAnimation:)])
    {
        [_shapeDelegate shape:self didStartAnimation:[anim valueForKey:kShapeKeyName]];
    }
}



//动画结束
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    self.beginTime=0;//恢复开始时间
    
    //飘落动画，需要调整Z轴
    if([kShapeAnimationFloat isEqualToString:[anim valueForKey:kShapeKeyName]])
    {
        self.zPosition=self.zPosition-self.bounds.size.height;
    }
    
    //回调
    if(_shapeDelegate && [_shapeDelegate respondsToSelector:@selector(shape:didStopAnimation:finished:)])
    {
        [_shapeDelegate shape:self didStopAnimation:[anim valueForKey:kShapeKeyName] finished:flag];
    }
    
    
    //执行队列剩下的动画
    CAAnimation *nextAnimation=[_animationQueue get];
    
    if(nextAnimation)//有动画则继续执行
    {
        nextAnimation.beginTime=CACurrentMediaTime();//队列里的动画，取消延迟时间
        [self addAnimation:nextAnimation forKey:[nextAnimation valueForKey:kShapeKeyName]];
    }
    else//全部执行完毕
    {
        self.animation=nil;
        _animationStoped=NO;
        _animating=NO;
    }
}



//禁用隐式动画
- (id<CAAction>)actionForKey:(NSString *)event
{
    return nil;
}



- (void)dealloc
{
//    NSLog(@"shape dealloc");
    
    [self clearAnimations];
    
    [_animationQueue release];
    [_animation release];
    [_animationCalculationMode release];
    [_animationRotateMode release];
    [_animationFillMode release];
    [_animationTimingMode release];
    
    [super dealloc];
}


@end



